package com.hk.webapp.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.hk.webapp.annotations.AllowAnonymous;
import com.hk.webapp.annotations.SkipPermission;
import com.hk.webapp.bean.Test;
import com.hk.webapp.service.TestService;
import com.hk.webapp.vo.EditTestVo;
import com.hk.webapp.vo.PageVo;
import com.hk.webapp.vo.ResultVo;
import com.hk.webapp.vo.TestVo;
import io.swagger.annotations.ApiOperation;
import org.jaxen.expr.TextNodeStep;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@SkipPermission
@RestController
public class TestController  extends  AdminBaseController {


    @Resource
    private TestService testService;

    @AllowAnonymous
    @GetMapping("/Test/GetList")
    @ApiOperation("获得所有数据，不分页")
    public ResultVo GetList()
    {
        List<Test> testList=testService.GetList();
        //String Result=GetCameraPreviewURL();
        return   ResultVo.createBySuccessData(testList);
    }
    @PostMapping("/Test/GetDetail")
    @SkipPermission
    @AllowAnonymous
    @ApiOperation("根据id获得详情")
    public   ResultVo  getDetail(Integer  id)
    {
        if(id<0)
        {
            return   ResultVo.createByError("参数非法");
        }
         Test  test= testService.GetDetail(id);
        return    ResultVo.createBySuccessData(test);

    }
    @PostMapping("/Test/GetPageList")
    @SkipPermission
    @AllowAnonymous
    @ApiOperation("获得分页数据")
    public   ResultVo  getPageList(@RequestBody PageVo pageVo)
    {
         PageInfo<Test> plist= testService.getPageList(pageVo);
         List<Test> list=plist.getList();
         return  ResultVo.createBySuccessData(plist,plist.getTotal());
    }

    @PostMapping("/Test/Add")
    @SkipPermission
    @AllowAnonymous
    @ApiOperation("新增数据")
    public   ResultVo  add(@RequestBody  @Valid  TestVo vo)  throws  Exception
    {
        //@Valid
        Test  test=new Test();
        //给属性赋值
        test.setName(vo.getName());

        //test.setCreateTime(vo.getCreateDate()==null? new Date() :vo.getCreateDate());
        testService.Insert(test);
        return ResultVo.createBySuccessData("succeed");
    }
    @ApiOperation("删除数据")
    @PostMapping("/Test/Del")
    @SkipPermission
    @AllowAnonymous
    public   ResultVo  del(int id)
    {
        if (id<=0)
        {
            return  ResultVo.createByError("删除id须大于0");
        }
        int count=  testService.Del(id);
        return  ResultVo.createBySuccessData("success",(long)count);
    }

    @ApiOperation("编辑数据")
    @PostMapping("/Test/Edit")
    @SkipPermission
    @AllowAnonymous
    public   ResultVo edit(@RequestBody  @Valid EditTestVo vo)
    {
        if (vo.getId()<=0)
        {
            return  ResultVo.createByError("id不能小于0");
        }
        //检查是否存在改角色
        Test tModel= testService.GetDetail(vo.getId());
        if (tModel==null)
        {
            return  ResultVo.createByError("数据不存在");
        }
        //如果数据存在则进行修改
        tModel.setName(vo.getName());
        //进行修改

        if (testService.updateById(tModel)>0)
        {
            return   ResultVo.createBySuccessData("success");
        }
        else
        {
            return   ResultVo.createByError("error");
        }
    }



    public static String GetCameraPreviewURL() {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = "127.0.0.1:443"; // artemis网关服务器ip端口
        ArtemisConfig.appKey = "29180881";  // 秘钥appkey
        ArtemisConfig.appSecret = "XO0wCAYGi4KV70ybjznx";// 秘钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + "/api/video/v1/cameras/previewURLs";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", "748d84750e3a4a5bbad3cd4af9ed5101");
        jsonBody.put("streamType", 0);
        jsonBody.put("protocol", "rtsp");
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "streamform=ps");
        String body = jsonBody.toJSONString();
        /**
         * STEP6：调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        return result;
    }


}
